package ericXie.com.command 
{

	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import ericXie.com.bussiness.LoadImgDelegate;
	import ericXie.com.events.LoadImageEvent;
	import ericXie.com.interfaces.IDataResponder;
	import ericXie.com.model.MainModel;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.TextEvent;
	import flash.system.Security;
	import flash.utils.Dictionary;

	
	/**
	 * 加载外部图片，以及加载进度时的Loading
	 * @author EricXie
	 */
	public class LoadImgCommand implements ICommand, IDataResponder 
	{
		//接口
		private var _delegate:LoadImgDelegate;
		//存储器
		private var _model:MainModel = MainModel.getInstance();
		//图片放置的容器
		private var imgContainer:DisplayObjectContainer;
		//图片名称
		private var imgName:String;
		//载入进度的图标
		private var loadIcon:Sprite = new Sprite();
		//载入容器的宽
		private var containerWidth:Number;
		//载入容器的高
		private var containerHeight:Number;
	
		public function LoadImgCommand() 
		{
			_delegate = new LoadImgDelegate(this);
		}
		/**
		 * 
		 * @param	event
		 * 赋值图片容器，容器宽高，加载图片
		 */
		public function execute(event:CairngormEvent):void
		{
			Security.allowDomain("*");
			Security.loadPolicyFile("http://pimg.damai.cn/crossdomain.xml");
			var readData:Object = event.data as Object;
			imgName = readData.name;
			imgContainer = readData.imgContainer
			containerWidth = readData.containerWidth;
			containerHeight = readData.containerHeight;
			if (readData.imgPath != null) {
				_delegate.readConfig(readData.imgPath);
			}
				

			
		}
		/**
		 * 加载完成后
		 * @param	evt
		 * 
		 * 这里判断，如果是带容器的，把加载的图片放在容器里，然后通过容器传递消息，让别人知道是否添加成功。
		 * 
		 */
		public function result(evt:Event):void
		{
			var loader:LoaderInfo = evt.target as LoaderInfo;
			if (imgContainer != null) {
				var disp:DisplayObject = loader.content as DisplayObject;
				disp.width = 50;
				disp.height = 50;
				imgContainer.addChild(disp);
				
				//imgContainer.dispatchEvent(new Event(imgName));
			}
			
		}
		public function fault(evt:IOErrorEvent):void
		{
			trace(evt.text)
		}

		public function progress(evt:ProgressEvent):void
		{
			
		}
		
	}

}